import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/CartPages/cart_page.dart';
import 'package:znmm/UI/CategoryPages/category_page.dart';
import 'package:znmm/UI/A_Common/buttons/easy_button/easy_button_demo.dart';
import 'package:znmm/UI/Other_Demo/home2_demo.dart/home_page2.dart';
import 'package:znmm/UI/Other_Demo/profile_page.dart';
import 'package:znmm/UI/PayplyPages/payply_page.dart';
import 'package:znmm/UI/utils/app_values.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/generated/l10n.dart';
import '../config/app_asset.dart';
import '../config/bloc/app_language_cubit.dart';
import 'HomePages/home_page.dart';
import 'MinePages/mine_page.dart';
import 'NewsPages/news_page.dart';
import 'ToolBoxPages/tool_box_page.dart';

class BottomNavigation extends StatefulWidget {
  const BottomNavigation({Key? key}) : super(key: key);
  @override
  // ignore: library_private_types_in_public_api
  _BottomNavigationDemoState createState() => _BottomNavigationDemoState();
}

class _BottomNavigationDemoState extends State<BottomNavigation>
    with TickerProviderStateMixin {
  int _currentIndex = 0;
  bool _hasRequestedAutomation = false; // 是否已经调用 AutomationMag().onRequest()

  final List<Widget> _children = [
    const HomePage2(),

    const HomePage(),
    // const CategoryPage(),
    // const CnCartPage(),
    // const MinePage(),
    //const PayplyPage(),
    //const ToolBoxPage(),
    const NewsPage(),
    const ProfilePage(),
    //const EasyButtonDemo(),
  ];

  @override
  void initState() {
    super.initState();

    // 使用Get.arguments获取传递的参数
    final arguments = Get.arguments;
    int iPageTag = arguments?['iPageTag'] ?? 0;
    if (_currentIndex < _children.length) {
      _onTabTapped(iPageTag);
    }
  }

  @override
  Widget build(BuildContext context) {
    MyGlobal.init(context);

    return BlocBuilder<AppLanguageCubit, AppLanguageState>(
        builder: (context, state) {
      List<BottomNavigationBarItem> itemsList = [];
      //首页
      itemsList.add(
          _buildBottomNavigationBarItem(S.of(context).home, AppAsset.tabHome));
      //分类
      itemsList.add(_buildBottomNavigationBarItem(
          S.of(context).category, AppAsset.tabCategory));
      //购物车
      itemsList.add(
          _buildBottomNavigationBarItem(S.of(context).cart, AppAsset.tabCart));

      //个人中心
      itemsList.add(_buildBottomNavigationBarItem(
        S.of(context).center,
        AppAsset.tabCenter,
      ));

      //other
      // itemsList.add(_buildBottomNavigationBarItem(
      //     S.of(context).other, AppAsset.tabOther));

      if (_children.length != itemsList.length) {
        return const Center(
          child: Text("error!"),
        );
      }

      return Scaffold(
        //drawer: CustomDrawer(), //抽屉
        //resizeToAvoidBottomInset: true,
        //body: _children[_currentIndex],

        body: IndexedStack(
          index: _currentIndex,
          children: _children,
        ),

        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          selectedItemColor: AppColor.black, // 选中状态颜色
          unselectedItemColor: AppColor.grey, // 未选中状态颜色
          selectedLabelStyle:
              const TextStyle(fontWeight: FontWeight.bold), // 选中样式
          unselectedLabelStyle: const TextStyle(fontWeight: FontWeight.normal),
          // selectedLabelStyle: const TextStyle(fontSize: 12),
          // unselectedLabelStyle: const TextStyle(fontSize: 12),
          type: BottomNavigationBarType.fixed,
          items: itemsList,
          onTap: _onTabTapped,
        ),
      );
    });
  }

  void _onTabTapped(int index) {
    setState(() {
      _currentIndex = index;

      // 如果是切换到 AutomationPage 并且尚未请求 AutomationMag().onRequest()
      if (index == 2 && !_hasRequestedAutomation) {
        //AutomationMag().onRequest();
        _hasRequestedAutomation = true; // 确保只调用一次
      }
    });
  }

  BottomNavigationBarItem _buildBottomNavigationBarItem(
      String label, String imagePath,
      {int notificationCount = -1} // 圆点的数量
      ) {
    return BottomNavigationBarItem(
      label: label,
      icon: Stack(
        alignment: Alignment.topRight,
        children: [
          Image.asset(
            imagePath,
            width: AppSize.s30,
            height: AppSize.s30,
            color: Colors.grey,
          ),
          if (notificationCount > 0) _redDot(notificationCount),
        ],
      ),
      activeIcon: Stack(
        alignment: Alignment.topRight,
        children: [
          Image.asset(
            imagePath,
            width: AppSize.s30,
            height: AppSize.s30,
            color: Colors.black,
          ),
          if (notificationCount > 0) _redDot(notificationCount),
        ],
      ),
    );
  }

  Widget _redDot(int iCount) {
    return Container(
      padding: const EdgeInsets.all(2),
      decoration: const BoxDecoration(
        color: Colors.red,
        shape: BoxShape.circle,
      ),
      child: Text(
        '$iCount',
        style: const TextStyle(
          color: Colors.white,
          fontSize: 12,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }
}
